/**
 * ' * All Rights Reserved , Copyright (C) 2020 , 青岛鼎信通讯股份有限公司
 * <p>
 * MeetPersonRecordController
 * 会议人员记录
 * <p>
 * 修改纪录
 * 2023-03-17 版本：1.0 xinfushuo 创建。
 *
 * @version 版本：1.0
 * @author 作者：xinfushuo
 * 创建日期：2023-03-17
 */

package com.topscomm.meet.controller;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.topscomm.basic.exception.ServiceException;
import com.topscomm.meet.MeetController;
import com.topscomm.meet.dto.ConfirmInfo;
import com.topscomm.meet.dto.ConfirmInfoParam;
import com.topscomm.meet.dto.PageParam;
import com.topscomm.meet.pojo.MeetManageEntity;
import com.topscomm.meet.pojo.MeetPersonRecordEntity;
import com.topscomm.meet.service.IMeetJoinConfirmService;
import com.topscomm.meet.service.IMeetPersonRecordService;
import com.topscomm.pub.util.StringUtil;
import com.topscomm.pub.vo.Page;
import com.topscomm.pub.vo.ResponseResult;
import groovy.util.logging.Log;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author: donghang
 * @date: 2020-10-31
 * @description:会议人员记录Controller
 * @modify:
 */
@Slf4j
@Log
@RestController
@RequestMapping("meet/joinConfirm")
public class MeetJoinConfirmController extends MeetController<MeetPersonRecordEntity, IMeetJoinConfirmService> implements IMeetJoinConfirmController {
    @Autowired
    IMeetPersonRecordService meetPersonRecordService;
    /**
     * 参会接口，传入sourcetye 和 sourceid 确认状态confirmState 原因 reason 补充说明 description ,以及邀请人员列表
     * 使用javax 进行校验
     *
     * @param
     * @return
     * @throws
     * @author xinfushuo
     * @date 2023/3/17
     * @modify
     */
    @PostMapping(value = "/confirm")
    public ResponseResult<Object> confirm(@Valid @RequestBody ConfirmInfo confirmInfo) {
        Boolean confirm = confirmInfo.getConfirmState();
        //进行反馈
        this.service.joinConfirm(confirmInfo);
        String message = datacodeCache.getValueDynamic("meetConfirmText", confirm.toString(), "code", "name");
        return ResponseResult.ok(message);
    }

    /**
     * 根据参会人员、sourcetype sourceid 获取展示信息
     *
     * @param
     * @return
     * @throws
     * @author xinfushuo
     * @date 2023/3/20
     * @modify
     */
    @GetMapping(value = "/loadJoinInfo")
    public ResponseResult<Object> loadJoinInfo(@RequestParam @NotBlank String sourceType, @NotBlank String sourceId, @NotBlank String userCode) {
        ConfirmInfo confirmInfo = ConfirmInfo.builder().sourceType(sourceType).sourceId(sourceId).userCode(userCode).build();
        confirmInfo = this.service.loadJoinInfo(confirmInfo);
        Map<String, Object> returnMap = new HashMap<>();
        Map<String, Object> confirmJoinMap = new HashMap<>();
        confirmJoinMap = this.service.convertToMap(confirmInfo);
        this.service.setDispFields(confirmJoinMap, datacodeCache);
        //应手机端要求，集合dataCodeList和表单信息。避免两次请求
        returnMap.put("confirmInfo", confirmJoinMap);
        returnMap.put("dataCodeList", this.getDataCodeList());
        return ResponseResult.ok(returnMap);
    }
    /**
     * 查询列表
     *
     * @param
     * @return
     * @throws
     * @author xinfushuo
     * @date 2023/3/21
     * @modify
     */
    @PostMapping(value = "/joinInfoList")
    public ResponseResult<Object> joinInfoList(@Valid @RequestBody ConfirmInfoParam param) {
        Map<String, Object> paraMap = this.service.convertToMap(param);
        //中文检测 区分是否工号姓名
        if (param.getQry_user() != null && !param.getQry_user().isEmpty()) {
            Boolean containName = param.getQry_user().codePoints().anyMatch(codepoint -> Character.UnicodeScript.of(codepoint) == Character.UnicodeScript.HAN);
            paraMap.put(Boolean.TRUE.equals(containName) ? "qry_name" : "qry_code", param.getQry_user());
        }
        //处理多选state
        if(param.getQry_state()!=null && !param.getQry_state().isEmpty()){
            paraMap.put("qry_state",JSONUtil.parseArray(param.getQry_state()).stream().map(s->s.toString().replace("\"","")).collect(Collectors.joining(",")));
        }
        paraMap.put("IsNeedPage", true);
        paraMap.put("begincount", param.getPageSize() * (param.getCurrentPage() - 1));
        paraMap.put("pagesize", param.getPageSize());
        List<Map<String, Object>> dataList = this.service.queryMap(paraMap, "querySubmitInfoForPage");
        this.service.setDispFields(dataList);
        paraMap.remove("IsNeedPage");
        int totalCount = this.service.getCount1(paraMap, "querySubmitInfoForPage");
        Page page = new Page(dataList, totalCount, param.getPageSize(), param.getCurrentPage());
        return ResponseResult.ok(page);
    }
    /**
     * @param paraMap
     * @return
     * @description:
     * @author: donghang
     * @date: 2020-10-31
     * @modify:
     */
    public Map<String, Object> getDataCodeList() {
        Map<String, Object> codeMap = new HashMap<>();
        List<Map<String, Object>> submitstateList = datacodeCache.getCodeListMap("MeetSubmitState", "", "sortcode");
        codeMap.put("submitstateList", submitstateList);
        List<Map<String, Object>> signstateList = datacodeCache.getCodeListMap("MeetSignState", "", "sortcode");
        codeMap.put("signstateList", signstateList);
        List<Map<String, Object>> reasonList = datacodeCache.getCodeListMap("JoinRefuseReason", "", "sortcode");
        codeMap.put("reasonList", reasonList);
        List<Map<String, Object>> enabledList = datacodeCache.getCodeListMap("CboYesNo", "", "sortcode");
        codeMap.put("enabledList", enabledList);
        return codeMap;
    }
    /**
      *
      * @author xinfushuo
      * @param
      * @throws
      * @return
      * @date 2023/3/22
      * @modify
      */
    @Override
    @GetMapping(value = "/dataCodeList")
    public ResponseResult<Object> dataCodeList(@RequestParam(required = false) Map<String, Object> paraMap) {
        Map<String, Object> codeMap = new HashMap<String, Object>();
        List<Map<String, Object>> enabledList = datacodeCache.getCodeListMap("CboYesNo", "", "sortcode");
        codeMap.put("enabledList", enabledList);
        codeMap.putAll(this.getDataCodeList());
        super.dataCodeList(paraMap);
        return ResponseResult.ok(codeMap);
    }
}
